true and false

In Unix-like operating systems, true and false are commands whose only function is to always return the value 0 or 1.

Contents

Usage

The exit status of a command is often used to indicate success (zero status) or failure (non-zero) of the command. The true and false commands represent the logical value of command success, because true returns 0, and false returns 1. The commands are usually employed in conditional statements and loops of shell scripts. For example, the following shell script repeats the echo hello loop until interrupted:

while true
do
  echo hello
done

The commands can be used to ignore the success or failure of a sequence of other commands, as in the example:

make&& false

Setting a user's login shell to false, in /etc/passwd, effectively denies them access to an interactive shell, but their account may still be valid for other services, such as FTP. (Although /sbin/nologin, if available, may be more fitting for this purpose, as it prints a notification before terminating the session.)

The programs take no "actual" parameters; in some versions, the standard parameter --help displays a usage summary and --version displays the program version.

Null command

The true command is sometimes substituted with the very similar to the null command,[1] written as a single colon (:). The null command is built into the shell, and may therefore be more efficient if true is an external program. The null command may take parameters, which are ignored. It is also used as a no-op dummy command for side-effects such as assigning default values to shell variables through the ${parameter:=word} parameter expansion form.[2] For example, from bashbug, the bug-reporting script for Bash:

: ${TMPDIR:=/tmp}
: ${EDITOR=$DEFEDITOR}
: ${USER=${LOGNAME-`whoami`}}

See also

References

  1. ^ "Colon", The Open group base specifications, issue 7, IEEE std 1003.1-2008, http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_16 
  2. ^ Cooper, Mendel (April 2011), "Null command", Advanced Bash-scripting guide, 6.3, The Linux documentation project, http://tldp.org/LDP/abs/html/special-chars.html#COLON0REF, retrieved 2011-08-04 

External links

Manual pages